package com.yanfan.modbus.frame.abs;

import com.yanfan.modbus.exception.ModbusTransportException;
import io.netty.buffer.ByteBuf;


abstract public class ModbusFrame {

    protected int slaveId;


    public ModbusFrame(int slaveId) throws ModbusTransportException {
        // 校验设备号,  0 是广播地址
        if (slaveId < 0 /* || slaveId > 247 */) {
            throw new ModbusTransportException("Invalid slave id", slaveId);
        }

        this.slaveId = slaveId;
    }


    public int getSlaveId() {
        return slaveId;
    }

    abstract public byte getFunctionCode();


    final public void write(ByteBuf buf) {
        buf.writeByte(slaveId);
        writeImpl(buf);
    }


    abstract protected void writeImpl(ByteBuf buf);
}
